package com.example.vendingmachine.service.impl;

import com.example.vendingmachine.dto.MachineDTO;
import com.example.vendingmachine.dto.MachineQuery;
import com.example.vendingmachine.entity.Machine;
import com.example.vendingmachine.entity.Point;
import com.example.vendingmachine.mapper.MachineMapper;
import com.example.vendingmachine.service.MachineService;
import com.example.vendingmachine.service.PointService;
import com.example.vendingmachine.exception.BusinessException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备服务实现类
 */
@Service
@RequiredArgsConstructor
public class MachineServiceImpl implements MachineService {

    private final MachineMapper machineMapper;
    private final PointService pointService;

    @Override
    public PageInfo<Machine> listMachines(MachineQuery query) {
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        return new PageInfo<>(machineMapper.selectList(query));
    }

    @Override
    public Machine getMachineById(Long id) {
        Machine machine = machineMapper.selectById(id);
        if (machine == null) {
            throw new BusinessException("设备不存在");
        }
        return machine;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createMachine(MachineDTO machineDTO) {
        // 检查点位是否存在且状态为启用
        checkPoint(machineDTO.getPointId());

        Machine machine = new Machine();
        BeanUtils.copyProperties(machineDTO, machine);
        // 设置状态为离线
        machine.setStatus("OFFLINE");

        machineMapper.insert(machine);
        return machine.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMachine(MachineDTO machineDTO) {
        // 检查设备是否存在
        Machine existMachine = getMachineById(machineDTO.getId());
        
        // 如果更换了点位
        if (!existMachine.getPointId().equals(machineDTO.getPointId())) {
            // 检查新点位
            checkPoint(machineDTO.getPointId());
        }

        Machine machine = new Machine();
        BeanUtils.copyProperties(machineDTO, machine);
        machineMapper.update(machine);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteMachine(Long id) {
        // 检查设备是否存在
        Machine machine = getMachineById(id);
        // 检查设备下是否有货道
        if (machineMapper.countSlotsByMachineId(id) > 0) {
            throw new BusinessException("设备下存在货道，无法删除");
        }
        machineMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMachineStatus(Long id, String status) {
        // 检查设备是否存在
        getMachineById(id);
        machineMapper.updateStatus(id, status);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateOnlineStatus(Long id, String status) {
        // 检查设备是否存在
        getMachineById(id);
        machineMapper.updateOnlineStatus(id, status);
    }

    @Override
    public boolean hasMachinesByPointId(Long pointId) {
        return machineMapper.countByPointId(pointId) > 0;
    }

    /**
     * 检查点位是否存在且状态为启用
     */
    private void checkPoint(Long pointId) {
        Point point = pointService.getPointById(pointId);
        if (!"ENABLED".equals(point.getStatus())) {
            throw new BusinessException("所选点位已被禁用");
        }
    }
} 